package com.acme.bankapp.domain.account;

import com.acme.bankapp.domain.exceptions.OverdraftLimitExceededException;


public class CheckingAccount extends AbstractAccount {
	private double overdraft;
	
	public CheckingAccount(int id, double amount, double overdraft) {
		super(id, amount);
		this.overdraft = overdraft;
	}
	
	public void deposit(double amount) {
		validateAmount(amount);
		balance += amount;
	}

	public void withdraw(double amount) throws OverdraftLimitExceededException {
		validateAmount(amount);
		if (balance + overdraft >= amount) {
			balance -= amount;
		} else {
			throw new OverdraftLimitExceededException(maximumAmountToWithdraw());
		}
	}

	@Override
	public double maximumAmountToWithdraw() {
		return balance + overdraft;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		final int otherPrime = 17;
		return getId() + (int) getBalance() * prime + (int) overdraft * otherPrime;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CheckingAccount other = (CheckingAccount) obj;
		if (getId() != other.getId())
			return false;
		if (getBalance() != other.getBalance())
			return false;
		if (overdraft != other.overdraft)
			return false;
		return true;
	}
	
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder("Checking account. Id = ");
		return builder.append(id).append(", balance = ").append(getBalance()).
				append(", overdraft = ").append(overdraft).toString();
	}
}
